﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UI;
using Base.Data;
using System.Linq;
using Base.Extensions;
public class SystemPanel : BasePanel {
    public Object systemitem;
    private Dictionary<string, Floor> floordic;
    private string[] keys;
    private Transform content;
	// Use this for initialization
	void Awake () {
        floordic = Dictionaries.systemmsg;
        keys = floordic.Keys.ToArray();
        content = transform.Find("Windows/Scroll View/Viewport/Content");
        Inits();
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public override void OnEnter()
    {
        gameObject.SetActive(true);
    }
    public override void OnExit()
    {
        gameObject.SetActive(false);
    }
    void Inits()
    {
        #region 初始化1.1
        foreach (var item in keys)
        {
            Transform form = systemitem.InstanUI(content);
            Floor floor = floordic.GetKeyValue<string, Floor>(item);
            form.Find("Title/Pipeline/Title").SetText(item);
            form.Find("Title/Toggle").SetToggleOnClick(delegate (bool ison)
            {
                if (ison == true)
                {
                    foreach (var name in floor.open)
                    {
                        GameObject _go = Dictionaries.FBXObject.GetKeyValue<string, GameObject>(name);
                        //Transform[] forms = _go.GetComponentsInChildren<Transform>();
                        foreach (Transform id in _go.transform)
                        {
                            if (Dictionaries.SingleObject.ContainsKey(id.name.GetID()))
                            {
                                GameObject go = Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id.name.GetID()).go;
                                go.SetActive(true);
                            }
                        }
                    }
                    print("勾选楼层" + item);
                }
                else
                {
                    foreach (var name in floor.off)
                    {
                        GameObject _go = Dictionaries.FBXObject.GetKeyValue<string, GameObject>(name);
                        //Transform[] forms = _go.GetComponentsInChildren<Transform>();
                        foreach (Transform id in _go.transform)
                        {
                            if (Dictionaries.SingleObject.ContainsKey(id.name.GetID()))
                            {
                                GameObject go = Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id.name.GetID()).go;
                                go.SetActive(false);
                            }
                        }
                    }
                    print("取消勾选楼层" + item);
                }
            }, true);
            Transform contentfloor = form.Find("Content");
            foreach (var emp in floor.emp)
            {
                Transform formemp = systemitem.InstanUI(contentfloor);
                formemp.Find("Title/Pipeline/Title").SetText(emp.name);
                formemp.Find("Title/Toggle").SetToggleOnClick(delegate (bool ison)
                {
                    if (ison == true)
                    {
                        foreach (var id in emp.id)
                        {
                            Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id).go.SetActive(true);
                        }
                        print("勾选大分类" + emp);
                    }
                    else
                    {
                        foreach (var id in emp.id)
                        {
                            Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id).go.SetActive(false);
                        }
                        print("取消勾选大分类" + emp);
                    }
                });
                Transform contenttype = formemp.Find("Content");
                foreach (var type in emp.type)
                {
                    Transform formtype = systemitem.InstanUI(contenttype);
                    formtype.Find("Title/Pipeline/Title").SetText(type.name);
                    formtype.Find("Title/Toggle").SetToggleOnClick(delegate (bool ison)
                    {
                        if (ison == true)
                        {
                            foreach (var id in type.id)
                            {
                                Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id).go.SetActive(true);
                            }
                            if (type.id.Count != 0)
                            {
                                Base.Custom.SingleObject single = Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(type.id[0]);
                                FreeCamera.freecamera.transform.position = new Vector3(single.point.x, single.point.y, single.point.z - 2);
                            }
                            print("勾选系统小分类" + type);
                        }
                        else
                        {
                            foreach (var id in type.id)
                            {
                                Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id).go.SetActive(false);
                            }
                            print("取消勾选系统小分类" + type);
                        }
                    });
                    Transform formsystem = formtype.Find("Content");
                    foreach (var system in type.system)
                    {
                        Transform formname = systemitem.InstanUI(formsystem);
                        formname.Find("Title/Pipeline/Title").SetText(system.name);
                        formname.Find("Title/Toggle").SetToggleOnClick(delegate (bool ison)
                        {
                            if (ison == true)
                            {
                                foreach (var id in system.id)
                                {
                                    Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id).go.SetActive(true);
                                }
                                print("勾选系统" + system);
                            }
                            else
                            {
                                foreach (var id in system.id)
                                {
                                    Dictionaries.SingleObject.GetKeyValue<string, Base.Custom.SingleObject>(id).go.SetActive(false);
                                }
                                print("取消勾选系统" + system);
                            }
                        });
                    }
                }
            }
        }
        #endregion

    }
}
